"""地面目标模块 - 用于定义地面目标点"""

import numpy as np
from astropy import units as u
from astropy.time import Time, TimeDelta
from poliastro.bodies import Earth
from poliastro.spheroid_location import SpheroidLocation
from typing import Tuple, Any

# 导入Observer抽象基类
from access import Observer, Observed

class GroundTarget(Observer, Observed):
    """地面目标类"""
    
    def __init__(self, name, latitude, longitude, height=0*u.m):
        """
        初始化地面目标
        
        参数:
            name: 目标名称
            latitude: 纬度 (度)
            longitude: 经度 (度)
            height: 高度 (默认为海平面)
        """
        self.name = name
        # 确保数值转换为天文单位
        if isinstance(latitude, (int, float)):
            self.latitude = latitude * u.deg
        else:
            self.latitude = latitude
            
        if isinstance(longitude, (int, float)):
            self.longitude = longitude * u.deg
        else:
            self.longitude = longitude
            
        self.height = height
        
        # 创建球形位置对象
        self.location = SpheroidLocation(self.longitude, self.latitude, self.height, Earth)
    
    def can_see(self, target, time:Time = None) -> Tuple[bool]:
        """
        判断地面站是否可以看到目标
        
        参数:
            target: 观测目标(通常是卫星)的itrs_xyz坐标
            
        返回:
            Tuple[bool]: (是否可见)
        """
        # 延迟导入，避免循环引用
        from access import AccessCalculator
        
        # 如果目标不是卫星，直接返回False
        if not hasattr(target, 'orbit'):
            return False
            
        # 检查基本可见性 (仰角和地球遮挡)
        itrs_xyz = target.get_itrs_xyz(time)
        if itrs_xyz is None:
            return False
        is_visible = AccessCalculator.is_visible_on_the_ground(itrs_xyz, self.location.cartesian_cords)
        
        return is_visible
        
    def get_itrs_xyz(self, time:Time = None)->u.Quantity:
        from astropy.coordinates import ITRS 
        return self.location.cartesian_cords
                
class GroundTargetGroup:
    """地面目标组"""
    
    def __init__(self, name, targets=None):
        """
        初始化地面目标组
        
        参数:
            name: 目标组名称
            targets: 目标列表
        """
        self.name = name
        self.targets = targets or []
        
    def add_target(self, target):
        """
        添加目标到组
        
        参数:
            target: 地面目标对象
        """
        self.targets.append(target)
        
    def __len__(self):
        """返回目标组中的目标数量"""
        return len(self.targets)
        
    def __getitem__(self, idx):
        """获取索引对应的目标"""
        return self.targets[idx]
        
    def __repr__(self):
        """字符串表示"""
        return f"GroundTargetGroup(name='{self.name}', targets={len(self.targets)})" 